﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Sustainalytics.Entities.RiskRatings;

namespace Sustainalytics.ClientDW.Synchronization.SyncRiskRatingsData
{
    public class CoreRiskRatingResearchProfileMapping
    {
        public static IEnumerable<CoreRiskRatingResearchProfile> Map(
            Dictionary<Guid, CorporateDataLogic.RiskRatingCorporateData> corporateDetails,
            IList<SharedEntities.RiskRatingsResearch.CoreRiskRatingResearchProfile> riskRatingResearchProfiles)
        {
            var entities = riskRatingResearchProfiles.Select(Mapper.Map<CoreRiskRatingResearchProfile>).ToList();

            Parallel.ForEach(entities, researchProfile =>
            {
                if (corporateDetails.ContainsKey(researchProfile.Id))
                {
                    researchProfile.CompanyName = corporateDetails[researchProfile.Id].CompanyName;
                    researchProfile.PeerGroup = corporateDetails[researchProfile.Id].PeerGroup;
                    researchProfile.SubPeerGroup = corporateDetails[researchProfile.Id].SubPeerGroup;
                    researchProfile.SubPeerGroupId = corporateDetails[researchProfile.Id].SubPeerGroupId;
                    researchProfile.Exchange = corporateDetails[researchProfile.Id].Exchange;
                    researchProfile.Ticker = corporateDetails[researchProfile.Id].Ticker;
                    researchProfile.ExchangeTicker = corporateDetails[researchProfile.Id].ExchangeTicker;
                    researchProfile.Country = corporateDetails[researchProfile.Id].Country;
                    researchProfile.MarketCapitalization = corporateDetails[researchProfile.Id].MarketCapitalization;
                }
            });

            return entities;
        }
    }
}
